<?php

namespace app\command;

use app\common\lib\Common;
use app\logic\cron\PlanLogic;
use bootstrap\DBListen;
use Webman\Config;
use app\common\lib\BaseCommand;
use app\logic\cron\DouyinLogic;
use app\logic\cron\UserLogic;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Webman\Event\Event;


class CronAutoGeneratePlan extends Command
{
    protected static $defaultName = 'cron:auto:generate:plan';
    protected static $defaultDescription = 'cron auto generate plan';

    /**
     * @return void
     */
    protected function configure()
    {
        //发布执行结束
        Event::dispatch('console.start', []);

        $this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
        Common::initTaskConfig();
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $name = $input->getArgument('name');
        $cmd = 'php webman '.self::$defaultName.($name?' '.$name:'');

        try{
            $startTime = round(microtime(true),4);

            $result = [];
            switch ($name){
                case 'autoGeneratePlan':
                    $result = PlanLogic::getInstance()->autoGeneratePlan();
                    break;
                case 'generateChapterContent':
                    $result = PlanLogic::getInstance()->generateChapterContent();
                    break;
                case 'autoGeneratePlanByUpload':
                    $result = PlanLogic::getInstance()->autoGeneratePlanByUpload();
                    break;
                case 'autoGenerateOverseaWork':
                    $result = PlanLogic::getInstance()->autoGenerateOverseaWork();
                    break;
            }

            $ttls =  round(round(microtime(true),4) - $startTime,4);
            //发布执行结束
            Event::dispatch('console.terminate', [$cmd,$result,$ttls]);

            return self::SUCCESS;
        }catch (\Throwable $e){
            //var_dump($e->getMessage());
            Common::consoleExecptionReport($cmd,$e);
            return self::FAILURE;
        }

    }


}
